package com.example.demo.controller;

import com.example.demo.enums.ResultCodeEnum;
import com.example.demo.mapper.BlogInfoMapper;
import com.example.demo.model.BlogInfo;
import com.example.demo.model.Result;
import com.example.demo.model.UserInfo;
import com.example.demo.service.BlogService;
import com.example.demo.service.UserInfoService;
import com.example.demo.utils.JwtUtils;
import com.example.demo.utils.SecurityUtils;
import io.jsonwebtoken.lang.Assert;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.graphql.GraphQlProperties;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService us;


    @Autowired
    private BlogService blogService;
    @RequestMapping("/login")
    public Result login(String userName,String password){
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return Result.fail(ResultCodeEnum.PARAM_INVALID.getCode(), "用户名或密码为空!");
        }
        UserInfo userInfo = us.selectByName(userName);
        if(userInfo==null || userInfo.getUserName()==null){
            return Result.fail(ResultCodeEnum.PARAM_INVALID.getCode(), "用户不存在");
        }

//        if(!password.equals(userInfo.getPassword())){
//            return Result.fail(ResultCodeEnum.PARAM_INVALID.getCode(),"密码错误");
//        }
        if(!SecurityUtils.verify(password,userInfo.getPassword())){
            log.error("密码验证失败");
            return Result.fail(ResultCodeEnum.PARAM_INVALID.getCode(),"密码错误");
        }
        //校验成功,发放令牌

        Map<String,Object> map= new HashMap<>();
        map.put("id",userInfo.getId()) ;
        map.put("userName",userInfo.getUserName());
        String jwt = JwtUtils.getJwt(map);
        //生成的令牌
        System.out.println("生成的令牌: "+jwt);
        return Result.success(jwt);
    }

    /**
     * 根据 token 获取 用户id
     * 再根据用户id 获取用户信息
     * @return
     */

    @RequestMapping("/getUserInfo")
    public UserInfo getUserInfo(HttpServletRequest request){
//        String token = request.getHeader("user_token");
//        Integer id = JwtUtils.getUserIdFromToken(token);
//        //根据用户id ,获取用户信息
//
//        return userInfoservice.getUserIoById(id);

        String userToken = request.getHeader("user_token");
        //从token中获取用户ID
        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        Assert.notNull(userId, "用户未登录");
        UserInfo userInfo = us.getUserIoById(userId);
        return userInfo;

    }

    /**
     * 从博客id 中获取作者信息
     *
     * 1. 根据博客id 获取博客信息
     * 2. 从博客信息中 获取用户id
     * 3. 根据用户ID 获取作者信息
     */


    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer blogId){
        Assert.notNull(blogId,"blogId 不能为null");
        BlogInfo blogInfo = blogService.getBlogDetail(blogId);
        Assert.notNull(blogInfo,"博客信息不能为 null");
        Integer userId = blogInfo.getUserId();
        Assert.notNull(userId,"userId不能为 null");
     return   us.getUserIoById(userId);
    }
}
